package com.wanmait.bootdemo.controller;

import com.wanmait.bootdemo.exception.CartypeNotFoundException;
import com.wanmait.bootdemo.pojo.Cartype;
import com.wanmait.bootdemo.service.CarTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
@RequestMapping("/manage/questionType")
public class QuestionmTypeController {
    @Resource
    CarTypeService carTypeService;

    @RequestMapping("list")
    public void list() {
        carTypeService.findAll();
    }

    //    抓住抛出异常
    @ExceptionHandler(CartypeNotFoundException.class)
    public ModelAndView process(CartypeNotFoundException e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        modelAndView.addObject("exception", e);
        return modelAndView;

    }

    @RequestMapping("view")
    @ModelAttribute("carType")
    public Cartype view(Integer id) {
        return carTypeService.findById(id);
    }
}
